package org.boulderfoodrescue.webapp.client.navigation;

import org.boulderfoodrescue.webapp.client.managers.LoginInfoManager;
import org.boulderfoodrescue.webapp.client.resources.Resources;
import org.boulderfoodrescue.webapp.shared.LoginInfo;
import org.boulderfoodrescue.webapp.shared.UserAccountDetails.AccessLevel;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.MenuItem;
import com.google.gwt.user.client.ui.Widget;

public class NavigationView extends Composite{

	private static NavigationPanelUiBinder uiBinder = GWT
			.create(NavigationPanelUiBinder.class);

	interface NavigationPanelUiBinder extends UiBinder<Widget, NavigationView> {
	}

	@UiField
	MenuItem home;
	@UiField
	MenuItem donors;
	@UiField
	MenuItem donorsDonors;
	@UiField
	MenuItem donorsNewDonor;
	@UiField
	MenuItem recipients;
	@UiField
	MenuItem recipientsRecipients;
	@UiField
	MenuItem recipientsNewRecipient;
	@UiField
	MenuItem pickups;
	@UiField
	MenuItem pickupsAllPickups;
	@UiField
	MenuItem pickupsMyPickups;
	@UiField
	MenuItem pickupsNewPickup;
	@UiField
	MenuItem pickupsEnterPickupReport;
	@UiField
	MenuItem options;
	@UiField
	MenuItem about;
	private MenuItem current;
	private MenuItem subcurrent;
	private String styleNameCurrent = Resources.INSTANCE.menubar().current();
	
	public NavigationView() {
		initWidget(uiBinder.createAndBindUi(this));
		setVisableMode();
		History.addValueChangeHandler(new ValueChangeHandler<String>(){
			@Override
			public void onValueChange(ValueChangeEvent<String> event) {
				setHyperlinkColor(event.getValue());
			}
		});
		setHyperlinkColor(History.getToken());
		setupClickCommands();
	}

	public void setupClickCommands(){
		setCommand(home, "home");
		setCommand(donors, "donors");
		setCommand(donorsDonors, "donors");
		setCommand(donorsNewDonor, "donorsNewDonor");
		setCommand(recipients, "recipients");
		setCommand(recipientsRecipients, "recipients");
		setCommand(recipientsNewRecipient, "recipientsNewRecipient");
		setCommand(pickups, "pickups");
		setCommand(pickupsAllPickups, "pickups");
		setCommand(pickupsMyPickups, "pickupsMyPickups");
		setCommand(pickupsNewPickup, "pickupsNewPickup");
		setCommand(pickupsEnterPickupReport, "pickupsEnterPickupReport");
		setCommand(options, "options");
		setCommand(about, "about");
	}
	
	public void setCommand(MenuItem menuItem, final String historyToken){
		menuItem.setCommand(new Command(){
			@Override
			public void execute() {
				History.newItem(historyToken, true);
			}
		});
	}
	
	public void setVisableMode()
	{
		LoginInfo loginInfo = LoginInfoManager.getLoginInfo();
		if(!loginInfo.isLoggedIn())
		{
			donors.setVisible(false);
			recipients.setVisible(false);
			pickups.setVisible(false);
			options.setVisible(false);
		}
		else
		{
			if(loginInfo.getAccessLevel().compareTo(AccessLevel.Volunteer) < 0)
				pickups.setVisible(false);
		}
	}

	private void setHyperlinkColor(String token)
	{
		if(current != null)
			current.removeStyleName(styleNameCurrent);
		if(subcurrent != null){
			subcurrent.removeStyleName(styleNameCurrent);
			subcurrent = null;
		}
		if(token.contains("pickups")){
			current = pickups;
			if(token.equals("pickups"))
				subcurrent = pickupsAllPickups;
			else if(token.equals("pickupsMyPickups"))
				subcurrent = pickupsMyPickups;
			else if(token.equals("pickupsEnterPickupReport"))
				subcurrent = pickupsEnterPickupReport;
			else if(token.equals("pickupsNewPickup"))
				subcurrent = pickupsNewPickup;
		}else if(token.contains("options"))
			current = options;
		else if(token.contains("about"))
			current = about;
		else if(token.contains("donors")){
			current = donors;
			if(token.equals("donors"))
				subcurrent = donorsDonors;
			else if(token.equals("donorsNewDonor"))
				subcurrent = donorsNewDonor;
		}else if(token.contains("recipients")){
			current = recipients;
			if(token.equals("recipients"))
				subcurrent = recipientsRecipients;
			else if(token.equals("recipientsNewRecipient"))
				subcurrent = recipientsNewRecipient;
		}else {
			current = home;
		}
		current.addStyleName(styleNameCurrent);
		if(subcurrent != null)
			subcurrent.addStyleName(styleNameCurrent);
	}
}
